package com.blogs.ui;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.RelativeLayout;

import com.blogs.component.BaseActivity;
import com.blogs.entity.CateFeed;
import com.blogs.entity.ListDate;
import com.blogs.service.SendBlogsService;
import com.blogs.tools.DBSharedPreferences;
import com.blogs.tools.HtmlFormat;
import com.cnblogs.R;
import com.cnblogs.R.color;

public class SubBlogPublicActivity extends BaseActivity implements OnClickListener, OnCheckedChangeListener {
	private DBSharedPreferences sp;
	
	private TextView subblog_chkDisplayHomePage_cb_tt;
	private TextView subblog_cbHomeCandidate_cb_tt;
	private TextView subblog_cbIsPublishToSiteHome_cb_tt;
	private TextView blog_categroy_tv_tt;
	
	private EditText subblog_txbTitle_et;
	private RelativeLayout subblog_chkDisplayHomePage_rl;
	private CheckBox subblog_chkDisplayHomePage_cb;
	private RelativeLayout subblog_cbHomeCandidate_rl;
	private CheckBox subblog_cbHomeCandidate_cb;
	private RelativeLayout subblog_cbIsPublishToSiteHome_rl;
	private CheckBox subblog_cbIsPublishToSiteHome_cb;
	private RelativeLayout subblog_channel_select;
	private int name_site_categroy_id;
	private TextView blog_categroy_tv;
	private EditText blog_txbTag_et;
	private EditText blog_txbExcerpt_et;
	private Button subblog_cancel_btn;
	private Button subblog_send_btn;
	private Toast toast;
	private String EditorBody;
	private RelativeLayout title_top_bg;
	private TextView title_include_title;
	private RelativeLayout sbp_body;
	private LinearLayout subblog_public_buttom_ll;
	private ArrayList<CateFeed > cateList;
	
	private int draft_id;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.sub_blog_pubilc);

		ViewGroup headview=(ViewGroup)findViewById(R.id.main_title_include);
		title_top_bg=(RelativeLayout)headview;
		title_include_title=(TextView)headview.findViewById(R.id.title_include_title);
		title_include_title.setText("详细设置");
		sbp_body=(RelativeLayout)findViewById(R.id.sbp_body);
		subblog_public_buttom_ll=(LinearLayout)findViewById(R.id.subblog_public_buttom_ll);
		
		sp = new DBSharedPreferences(this);
		
		subblog_chkDisplayHomePage_cb_tt=(TextView)findViewById(R.id.subblog_chkDisplayHomePage_cb_tt);
		subblog_cbHomeCandidate_cb_tt=(TextView)findViewById(R.id.subblog_cbHomeCandidate_cb_tt);
		subblog_cbIsPublishToSiteHome_cb_tt=(TextView)findViewById(R.id.subblog_cbIsPublishToSiteHome_cb_tt);
		blog_categroy_tv_tt=(TextView)findViewById(R.id.blog_categroy_tv_tt);
		
		subblog_txbTitle_et = (EditText) findViewById(R.id.subblog_txbTitle_et);
		subblog_chkDisplayHomePage_rl = (RelativeLayout) findViewById(R.id.subblog_chkDisplayHomePage_rl);
		subblog_chkDisplayHomePage_cb = (CheckBox) findViewById(R.id.subblog_chkDisplayHomePage_cb);
		subblog_cbHomeCandidate_rl = (RelativeLayout) findViewById(R.id.subblog_cbHomeCandidate_rl);
		subblog_cbHomeCandidate_cb = (CheckBox) findViewById(R.id.subblog_cbHomeCandidate_cb);
		subblog_cbIsPublishToSiteHome_rl = (RelativeLayout) findViewById(R.id.subblog_cbIsPublishToSiteHome_rl);
		subblog_cbIsPublishToSiteHome_cb = (CheckBox) findViewById(R.id.subblog_cbIsPublishToSiteHome_cb);
		subblog_channel_select = (RelativeLayout) findViewById(R.id.subblog_channel_select);
		blog_categroy_tv = (TextView) findViewById(R.id.blog_categroy_tv);
		blog_txbTag_et = (EditText) findViewById(R.id.blog_txbTag_et);
		blog_txbExcerpt_et = (EditText) findViewById(R.id.blog_txbExcerpt_et);
		subblog_cancel_btn = (Button) findViewById(R.id.subblog_cancel_btn);
		subblog_send_btn = (Button) findViewById(R.id.subblog_send_btn);

		subblog_chkDisplayHomePage_rl.setOnClickListener(this);
		subblog_cbHomeCandidate_rl.setOnClickListener(this);
		subblog_cbIsPublishToSiteHome_rl.setOnClickListener(this);
		subblog_channel_select.setOnClickListener(this);
		subblog_cancel_btn.setOnClickListener(this);
		subblog_send_btn.setOnClickListener(this);
		subblog_cbHomeCandidate_cb.setOnCheckedChangeListener(this);
		subblog_cbIsPublishToSiteHome_cb.setOnCheckedChangeListener(this);

		cateList=ListDate.getCatelList();
		
		this.InitValue();
		this.InitSkin();
	}
	private void InitSkin()
	{
		if(sp.GetIsDarkState())
		{
			subblog_txbTitle_et.getBackground().setLevel(2);
			title_top_bg.getBackground().setLevel(2);
			sbp_body.getBackground().setLevel(2);
			subblog_public_buttom_ll.getBackground().setLevel(2);
			blog_txbTag_et.getBackground().setLevel(2);
			blog_txbTag_et.setTextColor(Color.parseColor("#b1b1b1"));
			blog_txbExcerpt_et.getBackground().setLevel(2);
			blog_txbExcerpt_et.setTextColor(Color.parseColor("#b1b1b1"));
			subblog_chkDisplayHomePage_cb_tt.setTextColor(Color.parseColor("#b1b1b1"));
			subblog_cbHomeCandidate_cb_tt.setTextColor(Color.parseColor("#b1b1b1"));
			subblog_cbIsPublishToSiteHome_cb_tt.setTextColor(Color.parseColor("#b1b1b1"));
			blog_categroy_tv_tt.setTextColor(Color.parseColor("#b1b1b1"));
			subblog_txbTitle_et.setTextColor(getResources().getColor(color.con_text_night));
		}
		else
		{
			subblog_txbTitle_et.getBackground().setLevel(1);
			title_top_bg.getBackground().setLevel(1);
			sbp_body.getBackground().setLevel(1);
			subblog_public_buttom_ll.getBackground().setLevel(1);
			blog_txbTag_et.getBackground().setLevel(1);
			blog_txbTag_et.setTextColor(Color.parseColor("#000000"));
			blog_txbExcerpt_et.getBackground().setLevel(1);
			blog_txbExcerpt_et.setTextColor(Color.parseColor("#000000"));
			subblog_chkDisplayHomePage_cb_tt.setTextColor(Color.parseColor("#000000"));
			subblog_cbHomeCandidate_cb_tt.setTextColor(Color.parseColor("#000000"));
			subblog_cbIsPublishToSiteHome_cb_tt.setTextColor(Color.parseColor("#000000"));
			blog_categroy_tv_tt.setTextColor(Color.parseColor("#000000"));
			subblog_txbTitle_et.setTextColor(Color.parseColor("#000000"));
		}
	}
	private void InitValue() {
		draft_id=getIntent().getIntExtra("draft_id",0);
		EditorBody = getIntent().getStringExtra("EditorBody");
		subblog_txbTitle_et.setText(getIntent().getStringExtra("txbTitle"));
		name_site_categroy_id=getIntent().getIntExtra("name_site_categroy_id", -1);
		blog_categroy_tv.setText(name_site_categroy_id==-1?"请选择频道": cateList.get(name_site_categroy_id).cate_name );
		subblog_chkDisplayHomePage_cb.setChecked(getIntent().getBooleanExtra("chkDisplayHomePage", false));
		subblog_cbHomeCandidate_cb.setChecked(getIntent().getBooleanExtra("cbHomeCandidate", false));
		subblog_cbIsPublishToSiteHome_cb.setChecked(getIntent().getBooleanExtra("cbIsPublishToSiteHome", false));
		blog_txbTag_et.setText(getIntent().getStringExtra("txbTag"));
		blog_txbExcerpt_et.setText(getIntent().getStringExtra("txbExcerpt"));
	}

	private void Cancel() {
		Intent result = new Intent();
		result.putExtra("txbTitle", subblog_txbTitle_et.getText().toString().trim());
		result.putExtra("name_site_categroy_id", name_site_categroy_id);
		result.putExtra("chkDisplayHomePage", subblog_chkDisplayHomePage_cb.isChecked());
		result.putExtra("cbHomeCandidate", subblog_cbHomeCandidate_cb.isChecked());
		result.putExtra("cbIsPublishToSiteHome", subblog_cbIsPublishToSiteHome_cb.isChecked());
		result.putExtra("txbTag", blog_txbTag_et.getText().toString());
		result.putExtra("txbExcerpt", blog_txbExcerpt_et.getText().toString());
		setResult(RESULT_CANCELED, result);
		finish();
	}
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode == Activity.RESULT_OK) 
		{
			name_site_categroy_id=data.getIntExtra("name_site_categroy_id", -1);
			blog_categroy_tv.setText(name_site_categroy_id==-1?"请选择频道": cateList.get(name_site_categroy_id).cate_name );
		}
		else if(resultCode==Activity.RESULT_CANCELED)
		{
			
		}
	}
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if (v == subblog_chkDisplayHomePage_rl) {
			subblog_chkDisplayHomePage_cb.setChecked(!subblog_chkDisplayHomePage_cb.isChecked());
		} else if (v == subblog_cbHomeCandidate_rl) {
			subblog_cbHomeCandidate_cb.setChecked(!subblog_cbHomeCandidate_cb.isChecked());
		} else if (v == subblog_cbIsPublishToSiteHome_rl) {
			subblog_cbIsPublishToSiteHome_cb.setChecked(!subblog_cbIsPublishToSiteHome_cb.isChecked());
		} else if (v == subblog_channel_select) {
			Intent intent = new Intent(this, SubCateSelect.class);
			intent.putExtra("name_site_categroy_id", name_site_categroy_id);
			startActivityForResult(intent, 1);
		} else if (v == subblog_cancel_btn) {
			this.Cancel();
		} else if (v == subblog_send_btn) {
			if(subblog_txbTitle_et.length()==0)
			{
				this.showToast("亲,给文章加个标题吧!");
				return;
			}
			if(name_site_categroy_id==-1)
			{
				this.showToast("亲,给文章上个分类吧!");
				return;
			}
			Intent intent = new Intent(this, SendBlogsService.class);
			intent.putExtra("draft_id", draft_id);
			intent.putExtra("sid", sp.GetSessionID());
			intent.putExtra("txbTitle", subblog_txbTitle_et.getText().toString().trim());
			intent.putExtra("EditorBody", EditorBody);
			intent.putExtra("name_site_categroy",name_site_categroy_id==-1?"":cateList.get(name_site_categroy_id).cate_id+"");
			intent.putExtra("name_site_categroy_id", name_site_categroy_id);
			intent.putExtra("cbHomeCandidate", subblog_cbHomeCandidate_cb.isChecked());
			intent.putExtra("cbIsPublishToSiteHome", subblog_cbIsPublishToSiteHome_cb.isChecked());
			intent.putExtra("chkDisplayHomePage", subblog_chkDisplayHomePage_cb.isChecked());
			intent.putExtra("txbExcerpt", blog_txbExcerpt_et.getText().toString());
			intent.putExtra("txbTag", blog_txbTag_et.getText().toString());
			startService(intent);
			this.showToast("博文已进入发送队列");
			setResult(RESULT_OK);
			this.finish();
		}
	}

	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == event.KEYCODE_BACK) {
			this.Cancel();
		}
		return super.onKeyDown(keyCode, event);
	}

	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		// TODO Auto-generated method stub
		if (buttonView == subblog_cbHomeCandidate_cb) {
			if (subblog_cbHomeCandidate_cb.isChecked())
				subblog_cbIsPublishToSiteHome_cb.setChecked(false);
			if(!this.checkBodyLenth(EditorBody))
			{
				subblog_cbHomeCandidate_cb.setChecked(false);
				this.showToast("少于200字的文章不允许发布到首页候选区");
			}
		} else if (buttonView == subblog_cbIsPublishToSiteHome_cb) {
			if (subblog_cbIsPublishToSiteHome_cb.isChecked())
				subblog_cbHomeCandidate_cb.setChecked(false);
			if(!this.checkBodyLenth(EditorBody))
			{
				subblog_cbIsPublishToSiteHome_cb.setChecked(false);
				this.showToast("少于200字的文章不允许发布到网站首页");
			}
		}
	}

	private boolean checkBodyLenth(String txbBody) {
		int lenth = HtmlFormat.RemoveHtmlTag(txbBody).trim().length();
		if (lenth < 280)
			return false;
		else
			return true;

	}

	private void showToast(String text) {
		if (toast == null) {
			toast = Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT);
		} else {
			toast.setText(text);
			toast.setDuration(Toast.LENGTH_SHORT);
		}
		toast.show();
	}
}
